package com.example.test.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.example.test.dao.DataBaseHelper;
import com.example.test.model.social_api.NetAPI;
import com.example.test.model.social_api.QQAPI;
import com.example.test.model.social_api.SinaAPI;
import com.netease.microblog.oauth2.Oauth2;
import com.tencent.weibo.sdk.android.api.util.Util;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class SameFollowProcess {

	public static void giveSameResult(final Context context,final Handler handler)
	{
		new Thread(new Runnable() {
			
			@SuppressWarnings("unused")
			@Override
			public void run() {
				List<HashMap<String, String>> qqFollowList=getQQFollowList(context);
				List<HashMap<String, String>> sinaFollowList=getSinaFollowList();
				List<HashMap<String, String>> netFollowList=getNetFollowList();
				ArrayList<AccountUidInfo> data=getDataFromDataBase(context);
				FollowBoolean [] boolean1 =new FollowBoolean[data.size()];
				for(int i=0;i<data.size();i++)
				{
					FollowBoolean bool=new FollowBoolean();
					String qq=data.get(i).getQq();
					String sina=data.get(i).getSina();
					String net = data.get(i).getNet();
					if(qq!=null&&!qq.trim().equals("")&&qqFollowList!=null)
					bool.setQq(isInFollowList(data.get(i).getQq(), qqFollowList));
					if(sina!=null&&!sina.trim().equals("")&&sinaFollowList!=null)
					bool.setSina(isInFollowList(data.get(i).getSina(), sinaFollowList));
					if(net!=null&&!net.trim().equals("")&&netFollowList!=null)
					bool.setNet(isInFollowList(data.get(i).getNet(), netFollowList));
					bool.setUidInfo(data.get(i));
					boolean1[i]=bool;
				}
				Message msg=new Message();
				msg.what=Constants.MSG_GETFOLLOWLIST_SUCCESS;
				msg.obj=boolean1;
				handler.sendMessage(msg);
			}
		}).start();
	}
	public static boolean isInFollowList(String target_uid,List<HashMap<String, String>> list)
	{
		int i=0;
		for (HashMap<String, String> hashMap : list) {
			Log.v("test", i+"");
			if(i==49)
				System.out.println("");
			String uid =(String)hashMap.get("uid");
			if(uid.toLowerCase().equals(target_uid.toLowerCase()))
				return true;
			i++;
		}
		return false;
	}
	public static ArrayList<AccountUidInfo>  getDataFromDataBase(Context context)
	{
		ArrayList<AccountUidInfo> list=new ArrayList<AccountUidInfo>();
		SQLiteDatabase database=DataBaseHelper.getRedDatabase(context);
		Cursor cursor=  database.query("account", new String[]{"sina_uid","qq_uid","net_uid","name"}, "name!=? ", new String[]{GlobalVar.userName.toString()}, null, null, null);
		cursor.moveToFirst();
		for(int i=0;i<cursor.getCount();i++)
		{
			AccountUidInfo uidInfo =new AccountUidInfo();
			uidInfo.setSina(cursor.getString(0));
			uidInfo.setQq(cursor.getString(1));
			uidInfo.setNet(cursor.getString(2));
			uidInfo.setUserName(cursor.getString(3));
			list.add(uidInfo);
			cursor.moveToNext();
		}
		return list;
	}
	
	public static List<HashMap<String, String>> getQQFollowList(final Context context)
	{
		if(Util.getSharePersistent(context, "OPEN_ID")==null)
			return null;
		final boolean [] received=new boolean[1];
		final boolean [] finished=new boolean[1];
		final int []start=new int[1];
		received[0]=true;
		finished[0]=false;
		start[0]=0;
		final List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String,String>>();
		while(true)
		{
	         while(!received[0])
	         {
	        	 
	         }
	         if(finished[0]==true)
	        	 break;
	         received[0]=false;
	         new Thread(new Runnable() {
				
				@Override
				public void run() {
					Looper.prepare();
					QQAPI.getUserFollowList(context, Util.getSharePersistent(context,"OPEN_ID"), 30, start[0], new Handler(){

						@Override
						public void handleMessage(Message msg) {
							@SuppressWarnings("unchecked")
							List<HashMap<String, String>> tempList=(List<HashMap<String, String>>)msg.obj;
							if(tempList==null)
							{
								finished[0]=true;
							}
							else if(tempList.size()<30)
							{
								listArrays.addAll(tempList);
								received[0]=true;
								finished[0]=true;
							}
							else
							{
								listArrays.addAll(tempList);
								start[0]+=30;
								received[0]=true;
							}
						}
						
					});
					Looper.loop();
					
				}
			}).start();
	
		}
		return listArrays;
	}
	
	public static List<HashMap<String, String>> getSinaFollowList()
	{
		if(GlobalVar.sinaUid==null||GlobalVar.sinaUid.equals(""))
			return null;
		final boolean [] received=new boolean[1];
		final boolean [] finished=new boolean[1];
		final int []start=new int[1];
		received[0]=true;
		finished[0]=false;
		start[0]=0;
		final List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String,String>>();
		while(true)
		{
	         while(!received[0])
	         {
	        	 
	         }
	         if(finished[0]==true)
	        	 break;
	         received[0]=false;
	         new Thread(new Runnable() {
				
				@Override
				public void run() {
					Looper.prepare();
					SinaAPI.getFollowList( new Handler(){

						@Override
						public void handleMessage(Message msg) {
							@SuppressWarnings("unchecked")
							List<HashMap<String, String>> tempList=(List<HashMap<String, String>>)msg.obj;
							if(tempList==null)
							{
								finished[0]=true;
							}
							else if(tempList.size()<50)
							{
								listArrays.addAll(tempList);
								received[0]=true;
								finished[0]=true;
							}
							else
							{
								listArrays.addAll(tempList);
								start[0]+=50;
								received[0]=true;
							}
						}
						
					}, GlobalVar.sinaUid,start[0]);
					Looper.loop();
					
				}
			}).start();
	
		}
		return listArrays;
	}
	public static List<HashMap<String, String>> getNetFollowList()
	{
		if(Oauth2.getInstance().getAccessToken()==null||Oauth2.getInstance().getAccessToken().equals(""))
			return null;
		final boolean [] received=new boolean[1];
		final boolean [] finished=new boolean[1];
		final int []start=new int[1];
		received[0]=true;
		finished[0]=false;
		start[0]=0;
		final List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String,String>>();
		while(true)
		{
	         while(!received[0])
	         {
	        	 
	         }
	         if(finished[0]==true)
	        	 break;
	         received[0]=false;
	         new Thread(new Runnable() {
				
				@Override
				public void run() {
					Looper.prepare();
					NetAPI.getFollowList("", null, start[0], new Handler(){

						@Override
						public void handleMessage(Message msg) {
							@SuppressWarnings("unchecked")
							List<HashMap<String, String>> tempList=(List<HashMap<String, String>>)msg.obj;
							if(tempList==null)
							{
								finished[0]=true;
							}
							else if(tempList.size()<30)
							{
								listArrays.addAll(tempList);
								received[0]=true;
								finished[0]=true;
							}
							else
							{
								listArrays.addAll(tempList);
								start[0]+=30;
								received[0]=true;
							}
						}
						
					});
					Looper.loop();
					
				}
			}).start();
	
		}
		return listArrays;
	}
}
